package com.example.cet4try.activity;

import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;

import com.example.cet4try.R;
import com.example.cet4try.Utils.Tools;
import com.example.cet4try.Utils.WordData;
import com.example.cet4try.myDialog.WordDetailsDialog;

public class ActivityBankedCloze extends BaiduTTSActivity {

    private TextView spelling_show,chinese_show;
    private Button button_en1;
    private Button button_en2;
    private Button button_en3;
    private Button button_en4;
    private WordData wordData;
    private int dui;
    private boolean endflag = false,wrongFlag = false;
    private WordDetailsDialog wordDetailsDialog;
    private String username;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_banked_cloze);

        initView();

        SharedPreferences preferences = getSharedPreferences("App_Shared_Data", 0);
        username = preferences.getString("username","");

        button_en1.setOnClickListener(new button_en_Onclick());
        button_en2.setOnClickListener(new button_en_Onclick());
        button_en3.setOnClickListener(new button_en_Onclick());
        button_en4.setOnClickListener(new button_en_Onclick());

        Button btn_rs = findViewById(R.id.renshi);
        btn_rs.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Tools.AddUserData(username,wordData.getMid(),"already");
                reStart();
            }
        });
        Button speechBtn = findViewById(R.id.langdux);
        speechBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                speak();
            }
        });

        //String lz_en = "Do you speak _______ ?";
        //spelling_show.setText(Html.fromHtml(lz_en));
        int id = Tools.MyRandom(1,4493);
        wordData = Tools.souID(id);
        Tools.AddUserData(username,wordData.getMid(),"learned");

        String[] example = getLiziData();
        spelling_show.setText(example[0]);
        chinese_show.setText(example[2]);
        String key = example[1];
        dui = Tools.MyRandom(1,4);
        setShowText(dui,key);

        for (int i = 1;i<=4;i++){
            if (i!=dui){
                int iderror = Tools.MyRandom(1,4493);
                String str = Tools.souID(iderror).getEnglish();
                setShowText(i,str);
            }
        }

        Button next = findViewById(R.id.next_pin);
        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                reStart();
            }
        });

    }

    private void initView(){
        spelling_show = findViewById(R.id.spelling_show);
        chinese_show = findViewById(R.id.chinese_show);
        button_en1 = findViewById(R.id.button_en1);
        button_en2 = findViewById(R.id.button_en2);
        button_en3 = findViewById(R.id.button_en3);
        button_en4 = findViewById(R.id.button_en4);
        wordDetailsDialog = new WordDetailsDialog("选词填空测试");
        wordDetailsDialog.setPlaySound(new WordDetailsDialog.PlaySound() {
            @Override
            public void speak_d() {
                speak();
            }
        });
    }

    private String[] getLiziData(){
        String example = wordData.getExample();
        String english = wordData.getEnglish();
        //int index = -1;
        String key = null;
        String[] res = {"lz","key","cn"};

        String[] strs = example.split("#");
        int i = Tools.MyRandom(0,strs.length/2);
        i *= 2;
        //for (int i = 0; i<strs.length; i++){
            //String temp;
            //if (i%2==0){
                String t = strs[i];
                String[] ts = t.split("^\\d\\.+|\\s|\\.$|,|\\?|!");
                for (int k = 1;k < ts.length;k ++){
                    String tt = ts[k].toLowerCase();
                    String en = english.toLowerCase();

                    if (en.length() > 1){
                        if (en.substring(en.length()-1).contains("e"))
                            en = en.substring(0,english.length()-1);
                    }

                    if (tt.indexOf(en) == 0){
                        /*if (en.length() <= 3 && tt.length() != en.length()){
                            continue;
                        }*/
                        key = ts[k];
                        t = t.replaceFirst(key,"______");
                    }

                }
                /*if (t.contains(" "+english))
                    t = "<font color='#497BFB'>" + t + font_end;*/
                res[0] = t.replaceFirst("^\\d\\.+","");//例子

            //}
            //else {
                res[2] = strs[i+1];//中文
            //}
            //stringBuilder.append(temp);
        //}
        //string = stringBuilder.toString();
        if (key == null){
            int id = Tools.MyRandom(1,4493);
            wordData = Tools.souID(id);
            return getLiziData();
        }

        res[1] = key;

        return res;
    }

    private void setShowText(int index,String str){
        switch (index){
            case 1:button_en1.setText(str);break;
            case 2:button_en2.setText(str);break;
            case 3:button_en3.setText(str);break;
            case 4:button_en4.setText(str);break;
            default:break;
        }
    }

    private void speak() {
        super.speak(wordData.getEnglish());
    }

    private class button_en_Onclick implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            if (endflag){
                return;
            }
            AddTimes();
            Button button = findViewById(R.id.button_cn1);
            int btn = 0;
            switch (view.getId()){
                case R.id.button_en1:btn=1;button =  findViewById(R.id.button_en1);break;
                case R.id.button_en2:btn=2;button =  findViewById(R.id.button_en2);break;
                case R.id.button_en3:btn=3;button =  findViewById(R.id.button_en3);break;
                case R.id.button_en4:btn=4;button =  findViewById(R.id.button_en4);break;
                default:break;
            }

            //english_show1.setCompoundDrawables(null, null, yesicon, null);
            //Drawable noicon = getResources().getDrawable(R.drawable.no);
            //noicon.setBounds(0,0,150,150);

            if (dui==btn){
                endflag = true;
                if (!wrongFlag)
                    AddRight();
                Drawable yesicon = getResources().getDrawable(R.drawable.yes);
                yesicon.setBounds(0,0,150,150);
                button.setCompoundDrawables(null, null, yesicon, null);
                wordDetailsDialog.setData(wordData);
                wordDetailsDialog.show(getSupportFragmentManager(),"WordDetailsDialog");
            }
            else {
                wrongFlag = true;
                Animation shake = AnimationUtils.loadAnimation(ActivityBankedCloze.this,R.anim.shake);
                button.startAnimation(shake);
                Tools.AddUserData(username,wordData.getMid(),"wrong");

            }


        }
    }

}
